﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Metal_Archives
{
    public partial class SearchTips : PhoneApplicationPage
    {
        CommonMethods cm = new CommonMethods();

        public SearchTips()
        {
            InitializeComponent();

            //Is the user connected to the internet?
            if (cm.isOnline())
            {
                //Get data from site
                WebClient wc = new WebClient();
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.DownloadStringAsync(new Uri("http://www.metal-archives.com/content/help?index=3"));
            }
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (cm.isOnlineE(e))
            {
                HtmlAgilityPack.HtmlDocument tips = new HtmlAgilityPack.HtmlDocument();
                tips.LoadHtml(e.Result);

                this.lblHelp.Text = cleanUpCode(
                    tips.DocumentNode.Descendants("dl").First().InnerHtml.ToString()
                );

                if (cm.isThemedark()) { this.lblHelp.Foreground = new SolidColorBrush(Colors.White); }
                else { this.lblHelp.Foreground = new SolidColorBrush(Colors.Black); }
            }
        }

        string cleanUpCode(string cleanme)
        {
            cleanme = cleanme.Replace("\t", "");
            cleanme = cleanme.Replace("\n", "");
            cleanme = cleanme.Replace("    ", "");
            cleanme = cleanme.Replace("</dt><dd>", "\n\n");
            cleanme = cleanme.Replace("</dd><dt>", "\n\n");
            cleanme = cleanme.Replace("<em>", "");
            cleanme = cleanme.Replace("</em>", "");
            cleanme = cleanme.Replace("<dt>", "");
            cleanme = cleanme.Substring(0, cleanme.IndexOf("</dd> <h3>"));

            return cleanme;
        }
    }
}